Console
copy
1 | let x = {a:1,b:2,test:() =>{}} |
console.dir
查看所有对象的属性和方法
1 | console.dir(x) |
console.error
提供栈追踪
1 | console.error() |
console.timeEnd
code耗时
1 | console.time() |
console.log
所有obj 继承object toString
1 | # 作为对象或字符串进行打印 |
Array
of
1 | # Array.of(element0[, element1[, ...[, elementN]]]) |
from
Array.from() lets you create Arrays from:
array-like objects (objects with a length property and indexed elements); or
iterable objects (objects such as Map and Set).
1 | # Array.from(arrayLike [, mapFn [, thisArg]]) |
concat
不仅限于数组,还可以是任何值。
基本类型,copy value,修改不会对原数组产生影响;对象类型,copy reference,修改会对原对象产生影响。
1 | # const new_array = old_array.concat([value1[, value2[, ...[, valueN]]]]) |
filter
callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values
1 | # let newArray = arr.filter(callback(element[, index, [array]])[, thisArg]) |
slice
切片,不影响原来数组
1 | # arr.slice([start[, end]]) |
isArray
1 | Array.isArray([1, 2, 3]); // true |
every
1 | const isBelowThreshold = (currentValue) => currentValue < 40; |
1 | [12, 54, 18, 130, 44].every(x => x >= 10); // true |
some
查找满足特定逻辑的元素,是否存在
1 | # arr.some(callback(element[, index[, array]])[, thisArg]) |
修改元素,会立即生效
1 | const array = [1, 2, 3, 4, 5]; |
find
返回满足条件的第一个元素。 即使元素没有初始化,也会参与过滤
1 | # arr.find(callback(element[, index[, array]])[, thisArg]) |
findIndex
返回满足条件的第一个元素下标。 即使元素没有初始化,也会参与过滤,且不会影响原来的
1 | # arr.findIndex(callback( element[, index[, array]] )[, thisArg]) |
entries
1 | # 解构 |
1 | #二位数组排序 |
forEach
callback is not invoked for index properties that have been deleted or are uninitialized
1 | # arr.forEach(callback(currentValue [, index [, array]])[, thisArg]) |
values
values ,迭代value
keys, 迭代index
1 | # arr.values() |
flat
抽取子元素,按原来顺序,组合后返回
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 # var newArray = arr.flat([depth]);,default 1
var arr1 = [1, 2, [3, 4]];
arr1.flat(); // 默认1
// [1, 2, 3, 4]
var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]
# 剔除空元素
const arr5 = [1, 2, , 4, 5];
arr5.flat();
// [1, 2, 4, 5]
map+flat
1 | # var new_array = arr.flatMap(function callback(currentValue[, index[, array]]) { |
reduce
initialValue 是推荐提供的
If initialValue is not provided, reduce() will execute the callback function starting at index 1, skipping the first index. If initialValue is provided, it will start at index 0.
1 | # arr.reduce(callback( accumulator, currentValue[, index[, array]] )[, initialValue]) |
reduceRight
1 | # arr.reduceRight(callback(accumulator, currentValue[, index[, array]])[, initialValue]) |
fill
1 | # arr.fill(value[, start[, end]]) |
splice
替换或者添加元素,影响原数组
1 | # let arrDeletedItems = array.splice(start[, deleteCount[, item1[, item2[, ...]]]]) |
copyWithin
copy + part Change,不影响原来的
1 | # arr.copyWithin(target[, start[, end]]) |
shift
删除数组第一个元素,修改原数组
pop 删除的是数组,最后一个元素,也修改原数组
1 | # arr.shift() |
unshift
开始位置添加元素
1 | # arr.unshift(element1[, ...[, elementN]]) |
Date
1 | Date.now() |
2.时间字符串解析
1 | # 推荐,还是手动解析, |
3.时区偏差
1 | # 如果本地时区先于协调世界时,则该差值为正值,如果后于协调世界时则为负值 |
Set
1.从[]得到集合set
1 | # 常用于数组出重 |
2.判断是否包含某一项-has
1 | console.log(set1.has(5)); |
3.entries
1 | var mySet = new Set(); |
4. values
1 | var mySet = new Set(); |
String
1.统计字符- charCodeAt
1 | var count = 0; |
2.校验是否存在匹配-match
1 | # 未找到匹配则为null |
3.子字符搜索
1 | var str = 'To be, or not to be, that is the question.'; |
4. 返回所有匹配-matchAll
1 | let regexp = /t(e)(st(\d?))/g; |
5. 截取子字符串,
1 | # 不影响原有的 |
6.删除字符串两边空白- trim
1 | const greeting = ' Hello world! '; |
proxy
定制返回值
1 | let products = new Proxy({ |
类型安全
||
1 | a = a || b |
??
1 | const a = undefined; |
更多推荐
Angular开发利器
Angular8深入了解Directive指令
Angular8 HttpClient 30分钟深入了解下
参考
array
????](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_Precedence#Table)
若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏